<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/%E6%AD%A6%E6%B1%8932x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/%E6%AD%A6%E6%B1%8916x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib1/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/lib1/pace/pace-theme-center-simple.min.css">
  <script src="/lib1/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="查找查找表：是由同一类型的数据元素（或记录）构成的集合。关键字Key是数据元素中某个数据项的值，又称为键值，用它可以标识一个数据元素。若此关键字可以唯一地标识一个记录，则称此关键字为主关键字。对于哪些可以识别多个数据元素的关键字，我们称为次关键字。 查找：就是根据给定的某个值，在查找表中确定一个其关键字等于给定值的数据元素。 查找表的操作方式大致分为两种，静态查找表与动态查找表。静态查找表又称：只">
<meta property="og:type" content="article">
<meta property="og:title" content="查找与排序">
<meta property="og:url" content="http://example.com/2021/09/30/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E6%9F%A5%E6%89%BE%E4%B8%8E%E6%8E%92%E5%BA%8F/index.html">
<meta property="og:site_name" content="Technological Blog">
<meta property="og:description" content="查找查找表：是由同一类型的数据元素（或记录）构成的集合。关键字Key是数据元素中某个数据项的值，又称为键值，用它可以标识一个数据元素。若此关键字可以唯一地标识一个记录，则称此关键字为主关键字。对于哪些可以识别多个数据元素的关键字，我们称为次关键字。 查找：就是根据给定的某个值，在查找表中确定一个其关键字等于给定值的数据元素。 查找表的操作方式大致分为两种，静态查找表与动态查找表。静态查找表又称：只">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-30T04:18:39.000Z">
<meta property="article:modified_time" content="2021-10-19T09:18:41.135Z">
<meta property="article:author" content="Li Yudong">
<meta property="article:tag" content="设计模式">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://example.com/2021/09/30/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E6%9F%A5%E6%89%BE%E4%B8%8E%E6%8E%92%E5%BA%8F/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>查找与排序 | Technological Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Technological Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">IT小白的成长之旅</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="tags fa-fw"></i>标签<span class="badge">18</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="th fa-fw"></i>分类<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="archive fa-fw"></i>归档<span class="badge">95</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/tiarmor1" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/09/30/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E6%9F%A5%E6%89%BE%E4%B8%8E%E6%8E%92%E5%BA%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/author.jpg">
      <meta itemprop="name" content="Li Yudong">
      <meta itemprop="description" content="">
    </span>
    
    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Technological Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          查找与排序
        </h1>
    
        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
    
              <time title="创建时间：2021-09-30 12:18:39" itemprop="dateCreated datePublished" datetime="2021-09-30T12:18:39+08:00">2021-09-30</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-10-19 17:18:41" itemprop="dateModified" datetime="2021-10-19T17:18:41+08:00">2021-10-19</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">设计模式</span></a>
                </span>
            </span>
    
          
    
        </div>
      </header>
    
    
    
    
    <div class="post-body" itemprop="articleBody">
    
      
        <h1 id="查找"><a href="#查找" class="headerlink" title="查找"></a>查找</h1><p>查找表：是由同一类型的数据元素（或记录）构成的集合。关键字Key是数据元素中某个数据项的值，又称为键值，用它可以标识一个数据元素。若此关键字可以唯一地标识一个记录，则称此关键字为主关键字。对于哪些可以识别多个数据元素的关键字，我们称为次关键字。</p>
<p>查找：就是根据给定的某个值，在查找表中确定一个其关键字等于给定值的数据元素。</p>
<p>查找表的操作方式大致分为两种，静态查找表与动态查找表。静态查找表又称：只作查找操作的查找表，1、查询某个特定的数据元素是否在查找表中；2、检索某个特定的数据元素和各种属性。</p>
<p>动态查找表：在查找过程中同时插入查找表中不存在的数据元素，或者从查找表中删除已经存在的某个数据元素。</p>
<h2 id="顺序表查找"><a href="#顺序表查找" class="headerlink" title="顺序表查找"></a>顺序表查找</h2><p>顺序表查找是最基本的查找，又称为遍历查找，时间复杂度为O(n)。</p>
<h2 id="有序表查找"><a href="#有序表查找" class="headerlink" title="有序表查找"></a>有序表查找</h2><h3 id="折半查找"><a href="#折半查找" class="headerlink" title="折半查找"></a>折半查找</h3><p>折半查找又称二分查找，其本质是取中间值作为比较对象，能够通过数值间的关系，判断要查找的值究竟在中间值的左侧还是右侧，其实也不一定需要排序，只需要能知道在左侧还是右侧即可。折半查找的题目一般不难，重要的是要有这种思维，折半也不是必须要排序的。</p>
<p>折半算法的时间复杂度是O(logn)，它明显远远好于顺序查找的时间复杂度。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">给定一个按照升序排列的整数数组 nums，和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。</span></span><br><span class="line"><span class="comment">如果数组中不存在目标值 target，返回 [-1, -1]。</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number[]&#125;</span> <span class="variable">nums</span></span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;number&#125;</span> <span class="variable">target</span></span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return <span class="type">&#123;number[]&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> searchRange = <span class="function"><span class="keyword">function</span> (<span class="params">nums, target</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> mid, midL, midR;</span><br><span class="line">    <span class="comment">// 搜索右边界</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">searchR</span>(<span class="params">left, right, target</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">while</span> (left &lt;= right) &#123;</span><br><span class="line">            mid = (left + right) &gt;&gt; <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">if</span> (nums[mid] &lt;= target) left = mid + <span class="number">1</span>;</span><br><span class="line">            <span class="keyword">else</span> right = mid - <span class="number">1</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> right</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 在区间[0, nums.length - 1]搜索target的右边界midR</span></span><br><span class="line">    midR = searchR(<span class="number">0</span>, nums.length - <span class="number">1</span>, target)</span><br><span class="line">    <span class="comment">// midR &lt; 0说明超过边界；nums[midR] !== target说明无此元素；</span></span><br><span class="line">    <span class="keyword">if</span> (midR &lt; <span class="number">0</span> || nums[midR] !== target) <span class="keyword">return</span> [<span class="number">-1</span>, <span class="number">-1</span>]</span><br><span class="line">    <span class="comment">// 在区间[0, midR - 1]搜索target - 1的右边界midL</span></span><br><span class="line">    midL = searchR(<span class="number">0</span>, midR - <span class="number">1</span>, target - <span class="number">1</span>)</span><br><span class="line">    <span class="keyword">return</span> [midL + <span class="number">1</span>, midR]</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="插值查找"><a href="#插值查找" class="headerlink" title="插值查找"></a>插值查找</h3><p>查找时，为什么一定要折半，而不是折四分之一。我们可以根据范围大小，决定一下折半还是其他的遍历方式。</p>
<p>插值查找：是根据查找的关键字key与查找表中最大最小记录的关键字比较后的查找方法，其核心在于插值公式：(key - a[low])/(a[high] - a[low])。</p>
<p>mid = low + (high - low) * (key - a[low])/(a[high] - a[low])。</p>
<h3 id="斐波那契查找"><a href="#斐波那契查找" class="headerlink" title="斐波那契查找"></a>斐波那契查找</h3><p>本质上也是分隔点的不同。mid = low + F[K - 1] - 1。</p>
<h2 id="线性索引查找"><a href="#线性索引查找" class="headerlink" title="线性索引查找"></a>线性索引查找</h2><p>之间高效的查找方法都是基于数据有序的基础上的，然后事实上一些数据集会增长地特别快，例如微博等网站的评论，这些数据通常都是用先后顺序来进行存储的。对于这样的查找表，我们如何能够快速查找到需要的数据：索引。</p>
<p>索引就是把一个关键字与它对应的记录相关联的过程，一个索引由若干个索引项构成，每个索引项至少应包括关键字和其对应的记录在存储器中的位置等信息。</p>
<p>索引按照结构可分为线性索引、树形索引和多级索引。线性索引就是将索引项集合组织为线性结构，也称为索引表。</p>
<h3 id="稠密索引"><a href="#稠密索引" class="headerlink" title="稠密索引"></a>稠密索引</h3><p>稠密索引：在线性索引中，将数据集中的每个记录对应一个索引项。索引项是关键码有序的，这样就可以用有序表查找的方法。</p>
<h3 id="分块索引"><a href="#分块索引" class="headerlink" title="分块索引"></a>分块索引</h3><p>稠密索引因为索引项与数据集的记录个数相同，所以空间代价很大；为减少索引项的个数，我们可以对数据集进行分块，使其分块有序，然后再对每一块简历一个索引项，从而减少索引项的个数。</p>
<p>分块有序，是把数据集的记录分成了若干块，并且这些块要满足两个条件：1、块内无序；2、块间有序。</p>
<h3 id="倒排索引"><a href="#倒排索引" class="headerlink" title="倒排索引"></a>倒排索引</h3><p>索引表的通用结构：次关键码、记录号表。其中，记录号表存储具有相同次关键字的所有记录的记录号，这样的索引方法就是倒排索引。</p>
<p>倒排索引的优点显然是查找记录非常块，基本等于生成索引表后，查找时不用去读取记录，但它的缺点是该记录号不定长。</p>
<h2 id="二叉排序树"><a href="#二叉排序树" class="headerlink" title="二叉排序树"></a>二叉排序树</h2><p>二叉排序树又称二叉查找树，在堆排序时十分常见。这里我们主要还是写一下堆排序的代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*堆排序 */</span></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">      *<span class="doctag">@Description</span>:堆排序</span></span><br><span class="line"><span class="comment">      *<span class="doctag">@param </span>arr </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">headSort</span>(<span class="params">arr</span>)</span>&#123;</span><br><span class="line">        buildHeap(arr);<span class="comment">//构建堆</span></span><br><span class="line">        <span class="keyword">var</span> len = arr.length;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i=len<span class="number">-1</span>;i&gt;<span class="number">0</span>;i--)&#123;</span><br><span class="line">            swap(arr,<span class="number">0</span>,i);<span class="comment">//交换堆的第一个元素和最后一个元素</span></span><br><span class="line">            heapify(arr,i);<span class="comment">//调整堆</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> arr;</span><br><span class="line">    &#125;</span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * </span></span><br><span class="line"><span class="comment">          *<span class="doctag">@Description</span>:创建堆</span></span><br><span class="line"><span class="comment">          *<span class="doctag">@param <span class="variable">arr</span></span></span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">buildHeap</span>(<span class="params">arr</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> len = arr.length;</span><br><span class="line">        <span class="keyword">if</span>(len == <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="built_in">Math</span>.floor(len/<span class="number">2</span>) - <span class="number">1</span>;i&gt;<span class="number">0</span>;i--)&#123;</span><br><span class="line">            heapify(arr,i);<span class="comment">//在构建堆时，就需要一个个地调整堆</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * </span></span><br><span class="line"><span class="comment">          *<span class="doctag">@Description</span>:调整堆</span></span><br><span class="line"><span class="comment">          *<span class="doctag">@param </span>arr 调整数组</span></span><br><span class="line"><span class="comment">          *<span class="doctag">@param </span>i 跟</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">heapify</span>(<span class="params">arr,i</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> left = <span class="number">2</span>*i+<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">var</span> right = <span class="number">2</span>*i+<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">var</span> largest = i;</span><br><span class="line">        <span class="keyword">var</span> len = arr.length;</span><br><span class="line">        <span class="keyword">if</span>(left &lt;len &amp;&amp; arr[left]&gt;arr[largest])&#123;<span class="comment">//先判断左节点还否超出</span></span><br><span class="line">            largest=left;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(right &lt;len &amp;&amp; arr[right]&gt;largest)&#123;<span class="comment">//右节点是否超出 找出最大的子节点</span></span><br><span class="line">            largest=right;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(largest != i)&#123;</span><br><span class="line">            swap(arr,i,largest);<span class="comment">//交换 largrst为i，这里交换的是两者序数对应的值</span></span><br><span class="line">            heapify(arr,largest);</span><br><span class="line"><span class="comment">//递归调整，如果左侧为largest，那么将左侧节点提上去，会影响左侧子树的原本正确排序，因此。递归调整左侧；递归调用的参数是，为最大的那一个</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * </span></span><br><span class="line"><span class="comment">          *<span class="doctag">@Description</span>:交换</span></span><br><span class="line"><span class="comment">          *<span class="doctag">@param <span class="variable">arr</span></span></span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">swap</span>(<span class="params">arr,i,j</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> temp=arr[i];</span><br><span class="line">        arr[i] = arr[j];</span><br><span class="line">        arr[j] = temp;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="平衡二叉树"><a href="#平衡二叉树" class="headerlink" title="平衡二叉树"></a>平衡二叉树</h2><p>平衡二叉树是一种二叉排序树，其中每一个节点的左子树和右子树的高度查至多等于1。</p>
<p>我们可以利用递归的方式，来判断一个二叉树是否是平衡二叉树。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for a binary tree node.</span></span><br><span class="line"><span class="comment"> * function TreeNode(val, left, right) &#123;</span></span><br><span class="line"><span class="comment"> *     this.val = (val===undefined ? 0 : val)</span></span><br><span class="line"><span class="comment"> *     this.left = (left===undefined ? null : left)</span></span><br><span class="line"><span class="comment"> *     this.right = (right===undefined ? null : right)</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;TreeNode&#125;</span> <span class="variable">root</span></span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return <span class="type">&#123;boolean&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> isBalanced = <span class="function"><span class="keyword">function</span> (<span class="params">root</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> balanced(root) !== <span class="number">-1</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> balanced = <span class="function"><span class="keyword">function</span> (<span class="params">node</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (!node) <span class="keyword">return</span> <span class="number">0</span></span><br><span class="line">  <span class="keyword">const</span> left = balanced(node.left)</span><br><span class="line">  <span class="keyword">const</span> right = balanced(node.right)</span><br><span class="line">  <span class="keyword">if</span> (left === <span class="number">-1</span> || right === <span class="number">-1</span> || <span class="built_in">Math</span>.abs(left - right) &gt; <span class="number">1</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="number">-1</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Math</span>.max(left, right) + <span class="number">1</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>同样，我们也可以用下面的方式，给出一个二叉搜索树，将其变为平衡二叉树。</p>
<p>下面的第一个做法其实有些取巧了。重新获取了整个树的结点值，再对其排序并重新构建二叉树，时间复杂度会比较高，更好的方法和思路应该是后面那一个。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> balanceBST = <span class="function"><span class="keyword">function</span>(<span class="params">root</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 获取节点值。bst中序遍历返回升序数组。</span></span><br><span class="line">    <span class="keyword">let</span> arr = [];</span><br><span class="line">    <span class="keyword">let</span> fn = <span class="function">(<span class="params">node</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span>(!node)<span class="keyword">return</span>;</span><br><span class="line">        fn(node.left);</span><br><span class="line">        arr.push(node.val);</span><br><span class="line">        fn(node.right);</span><br><span class="line">    &#125;;</span><br><span class="line">    fn(root);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 重建平衡bst</span></span><br><span class="line">    <span class="keyword">let</span> mid = ~~(arr.length / <span class="number">2</span>);</span><br><span class="line">    <span class="keyword">let</span> h = <span class="keyword">new</span> TreeNode(arr[mid]);</span><br><span class="line">    <span class="keyword">let</span> buildTree = <span class="function">(<span class="params">node, arr, mid</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span>(!arr.length || arr.length === <span class="number">1</span>)&#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(arr.length === <span class="number">2</span>)&#123; <span class="comment">// 跟mid的划分有关，有两个数，mid是右侧那个</span></span><br><span class="line">            node.left = <span class="keyword">new</span> TreeNode(arr[<span class="number">0</span>]);</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 左树</span></span><br><span class="line">        node.left = <span class="keyword">new</span> TreeNode(arr[~~(mid / <span class="number">2</span>)]);</span><br><span class="line">        buildTree(node.left, arr.slice(<span class="number">0</span>, mid), ~~(mid / <span class="number">2</span>));</span><br><span class="line"></span><br><span class="line">        <span class="comment">// 右树</span></span><br><span class="line">        <span class="keyword">let</span> a = arr.slice(mid+<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">let</span> mi = ~~(a.length / <span class="number">2</span>);</span><br><span class="line">        node.right = <span class="keyword">new</span> TreeNode(a[mi]);</span><br><span class="line">        buildTree(node.right, a, mi)</span><br><span class="line">    &#125;;</span><br><span class="line">    <span class="comment">// 参数解释：给定一个数组，已经锁定了head和head的index</span></span><br><span class="line">    buildTree(h, arr, mid);</span><br><span class="line">    <span class="keyword">return</span> h;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<p>解题思路：每一棵二叉树都要判断平衡因子，如果不平衡就调整它；<br>如果左边的平衡因子大就向右转移<br>如果右边的平衡因子大就向右转移<br>然后，再判断二叉树的左右子树的平衡因子是否符合平衡二叉树的性质</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Definition for a binary tree node.</span></span><br><span class="line"><span class="comment"> * function TreeNode(val) &#123;</span></span><br><span class="line"><span class="comment"> *     this.val = val;</span></span><br><span class="line"><span class="comment"> *     this.left = this.right = null;</span></span><br><span class="line"><span class="comment"> * &#125;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@param <span class="type">&#123;TreeNode&#125;</span> <span class="variable">root</span></span></span></span><br><span class="line"><span class="comment"> * <span class="doctag">@return <span class="type">&#123;TreeNode&#125;</span></span></span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> balanceBST = <span class="function"><span class="keyword">function</span> (<span class="params">root</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (root == <span class="literal">null</span>) <span class="keyword">return</span> root;</span><br><span class="line">    <span class="keyword">while</span> (<span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">let</span> leftHeight = get_num(root.left);</span><br><span class="line">        <span class="keyword">let</span> rightHeight = get_num(root.right);</span><br><span class="line">        <span class="keyword">let</span> sub = leftHeight - rightHeight;</span><br><span class="line">        <span class="keyword">if</span> (sub &gt;= <span class="number">-1</span> &amp;&amp; sub &lt;= <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> <span class="keyword">if</span> (sub &lt; <span class="number">-1</span>) &#123;</span><br><span class="line">            root = turn_left(root)</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            root = turn_right(root);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    root.left = balanceBST(root.left);</span><br><span class="line">    root.right = balanceBST(root.right);</span><br><span class="line">    <span class="keyword">return</span> root;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">turn_left</span>(<span class="params">root</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> currNode = root.right;</span><br><span class="line">    <span class="comment">// 找到root结点的最左边结点</span></span><br><span class="line">    find_min(currNode).left = root;</span><br><span class="line">    root.right = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">return</span> currNode;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">turn_right</span>(<span class="params">root</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> currNode = root.left;</span><br><span class="line">    <span class="comment">// 找到root结点的最右边结点</span></span><br><span class="line">    find_max(currNode).right = root;</span><br><span class="line">    root.left = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">return</span> currNode;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">find_min</span>(<span class="params">root</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (root.left == <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> root;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> node = root;</span><br><span class="line">    <span class="keyword">while</span> (node.left != <span class="literal">null</span>) &#123;</span><br><span class="line">        node = node.left;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> node;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">find_max</span>(<span class="params">root</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> node = root.right;</span><br><span class="line">    <span class="keyword">if</span> (node == <span class="literal">null</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> root;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span> (node.right != <span class="literal">null</span>) &#123;</span><br><span class="line">        node = node.right;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> node;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">get_num</span>(<span class="params">root</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!root) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> get_num(root.left) + <span class="number">1</span> + get_num(root.right);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="多路查找树（B树）"><a href="#多路查找树（B树）" class="headerlink" title="多路查找树（B树）"></a>多路查找树（B树）</h2><p>多路查找树：B树，其每一个结点的海子叔可以多余两个，且每一个结点处可以存储多个元素。</p>
<p>2-3树就是这样的一个查找树，其中的每一个结点都具有两个孩子或者3个孩子。一个2结点包含一个元素和两个孩子；一个3结点包含一大一小两个元素和三个孩子。</p>
<h3 id="B树"><a href="#B树" class="headerlink" title="B树"></a>B树</h3><p>B树是一种平衡的多路查找树，结点最大的孩子数目称为B树的阶。一个m阶的B树具有以下的属性：</p>
<p>1、如果一个结点不是叶结点，则其至少有两个子树；</p>
<p>2、每一个非根的分支节点都有k-1个元素和k个孩子，其中m/2 &lt;= k &lt;= m。每一个叶子节点都有k-1个元素。</p>
<p>3、所有叶子节点都位于同一层次。</p>
<p>4、所有分支节点包含以下信息数据(n,A0,K1,A1,A2,…,Kn,An)，其中Ki为关键字，且Ki&lt;Ki+1；Ai为指向子树根节点的指针，且指针Ai-1所指子树中所有结点的关键字均小于Ki，An所指子树中所有结点的关键字均大于Kn，n为关键字的个数。</p>
<p>在B树上查找的过程：是一个顺时针查找结点和在结点中查找关键字的交叉过程。</p>
<h3 id="B-树"><a href="#B-树" class="headerlink" title="B+树"></a>B+树</h3><p>B+树是应文件系统所需而出的一种B树的变形。在B+树中，出现在分支结点中的元素会被当做它们在该分支结点位置的中序后继者中再次列出。另外，每一个叶子结点都会保存一个指向后一叶子结点的指针。</p>
<h2 id="散列表"><a href="#散列表" class="headerlink" title="散列表"></a>散列表</h2><p>散列技术：是在记录的存储位置和它的关键字之间建立一个确定的对应关系f，使得每个关键字key对应的一个存储位置f。 这里我们把对应关系称为散列函数，又称hash函数。</p>
<h1 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h1><h2 id="排序的分类"><a href="#排序的分类" class="headerlink" title="排序的分类"></a>排序的分类</h2><p>稳定性：假设Ki=Kj，且在排序前的序列中Ri领先于Rj。如果排序后Ri仍领先于Rj，则称所用的排序方法是稳定的；反之，若可能使得排序后的序列中Rj领先于Ri，则称所用的排序方法是不稳定的。</p>
<p>内排序与外排序：</p>
<p>内排序：在整个排序过程中，待排序的所有记录全部被放置在内存中。外排序是由于排序的记录个数过多，不能同时放置在内存，整个排序过程需要在内外村之间多次交换数据才能进行。</p>
<h2 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h2><p>冒泡排序是一种交换排序，它基本思想是：两两比较相邻记录的关键字，如果反序列则交换，直到没有反序的记录为止。</p>
<p>其时间复杂度为O(n * n)。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*冒泡排序</span></span><br><span class="line"><span class="comment">1.比较相邻的元素。如果第一个比第二个大，就交换它们两个</span></span><br><span class="line"><span class="comment">2.对每一对相邻元素作同样的工作，从开始第一对到结尾的最后一对，这样在最后的元素应该会是最大的数</span></span><br><span class="line"><span class="comment">3.除去最右的元素，我们对剩余的元素做同样（1、2）的工作，如此重复下去，直到排序完成。</span></span><br><span class="line"><span class="comment">这里我们把排序的方法封装到Array的原型链对象上，这样随便一个数组就可以直接调用了 */</span></span><br><span class="line"><span class="built_in">Array</span>.prototype.bubbleSort = <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> len = <span class="keyword">this</span>.length</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> j=<span class="number">1</span>; j&lt;len; j++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i=<span class="number">0</span>; i&lt;len-j; i++) &#123;<span class="comment">//i &lt; len - j;用于确保每次循环均排好一个数，不再考虑</span></span><br><span class="line">            <span class="comment">// 开始比较，如果第一个数大于第二个数就进行交换，这样大的就到后面去了</span></span><br><span class="line">            <span class="keyword">if</span> (<span class="keyword">this</span>[i] &gt; <span class="keyword">this</span>[i+<span class="number">1</span>]) &#123;</span><br><span class="line">                <span class="keyword">let</span> tempnum = <span class="keyword">this</span>[i+<span class="number">1</span>]</span><br><span class="line">                <span class="keyword">this</span>[i+<span class="number">1</span>] = <span class="keyword">this</span>[i]</span><br><span class="line">                <span class="keyword">this</span>[i] = tempnum</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">let</span> arr = [<span class="number">2</span>,<span class="number">9</span>,<span class="number">5</span>,<span class="number">7</span>,<span class="number">1</span>,<span class="number">1</span>,<span class="number">6</span>,<span class="number">3</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"原来："</span>, arr.toString())</span><br><span class="line"><span class="comment">// 原来： 2,9,5,7,1,1,6,3,3,4</span></span><br><span class="line">arr.bubbleSort()</span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">"排序后："</span>, arr.toString())</span><br><span class="line"><span class="comment">// 排序后： 1,1,2,3,3,4,5,6,7,9</span></span><br></pre></td></tr></table></figure>

<h2 id="希尔排序"><a href="#希尔排序" class="headerlink" title="希尔排序"></a>希尔排序</h2><p>希尔排序并不是一种稳定的排序方式，</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*希尔排序</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">shellSort</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> len = arr.length;</span><br><span class="line">    <span class="comment">// gap 即为增量</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> gap = <span class="built_in">Math</span>.floor(len / <span class="number">2</span>); gap &gt; <span class="number">0</span>; gap = <span class="built_in">Math</span>.floor(gap / <span class="number">2</span>)) &#123;</span><br><span class="line">      <span class="keyword">for</span> (<span class="keyword">let</span> i = gap; i &lt; len; i++) &#123;<span class="comment">//对于后面的，都要进行插入排序，组内进行插入排序</span></span><br><span class="line">        <span class="keyword">let</span> j = i;</span><br><span class="line">        <span class="keyword">let</span> current = arr[i];<span class="comment">//对于每个新加进来的数，需要进行直接插入排序，即对其一个个地进行往前有序区试探，从而排序</span></span><br><span class="line">        <span class="keyword">while</span>(j - gap &gt;= <span class="number">0</span> &amp;&amp; current &lt; arr[j - gap]) &#123;<span class="comment">//没有实际上用数组来保存而实现分组，而实用gap来表示间隔，这样就能自动分开</span></span><br><span class="line">          arr[j] = arr[j - gap];</span><br><span class="line">          j = j - gap;</span><br><span class="line">        &#125;</span><br><span class="line">        arr[j] = current;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">var</span> arr = [<span class="number">3</span>,<span class="number">5</span>,<span class="number">7</span>,<span class="number">1</span>,<span class="number">4</span>,<span class="number">56</span>,<span class="number">12</span>,<span class="number">78</span>,<span class="number">25</span>,<span class="number">0</span>,<span class="number">9</span>,<span class="number">8</span>,<span class="number">42</span>,<span class="number">37</span>];</span><br><span class="line">  shellSort(arr);</span><br></pre></td></tr></table></figure>

<h2 id="堆排序"><a href="#堆排序" class="headerlink" title="堆排序"></a>堆排序</h2><p>堆是具有以下性质的二叉树：每个结点的值都大于或等于其左右孩子结点的值，称为大顶堆；或者每个结点的值都小于或等于其左右孩子结点的值，称为小顶堆。</p>
<p>由于记录的比较与交换是跳跃式进行，因此堆排序也是一种不稳定的排序方法。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*堆排序 */</span></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * </span></span><br><span class="line"><span class="comment">      *<span class="doctag">@Description</span>:堆排序</span></span><br><span class="line"><span class="comment">      *<span class="doctag">@param </span>arr </span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">headSort</span>(<span class="params">arr</span>)</span>&#123;</span><br><span class="line">        buildHeap(arr);<span class="comment">//构建堆</span></span><br><span class="line">        <span class="keyword">var</span> len = arr.length;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i=len<span class="number">-1</span>;i&gt;<span class="number">0</span>;i--)&#123;</span><br><span class="line">            swap(arr,<span class="number">0</span>,i);<span class="comment">//交换堆的第一个元素和最后一个元素</span></span><br><span class="line">            heapify(arr,i);<span class="comment">//调整堆</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> arr;</span><br><span class="line">    &#125;</span><br><span class="line">     <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * </span></span><br><span class="line"><span class="comment">          *<span class="doctag">@Description</span>:创建堆</span></span><br><span class="line"><span class="comment">          *<span class="doctag">@param <span class="variable">arr</span></span></span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">buildHeap</span>(<span class="params">arr</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> len = arr.length;</span><br><span class="line">        <span class="keyword">if</span>(len == <span class="number">0</span>)</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        <span class="keyword">for</span>(<span class="keyword">var</span> i=<span class="built_in">Math</span>.floor(len/<span class="number">2</span>) - <span class="number">1</span>;i&gt;<span class="number">0</span>;i--)&#123;</span><br><span class="line">            heapify(arr,i);<span class="comment">//在构建堆时，就需要一个个地调整堆</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * </span></span><br><span class="line"><span class="comment">          *<span class="doctag">@Description</span>:调整堆</span></span><br><span class="line"><span class="comment">          *<span class="doctag">@param </span>arr 调整数组</span></span><br><span class="line"><span class="comment">          *<span class="doctag">@param </span>i 跟</span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">heapify</span>(<span class="params">arr,i</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> left = <span class="number">2</span>*i+<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">var</span> right = <span class="number">2</span>*i+<span class="number">2</span>;</span><br><span class="line">        <span class="keyword">var</span> largest = i;</span><br><span class="line">        <span class="keyword">var</span> len = arr.length;</span><br><span class="line">        <span class="keyword">if</span>(left &lt;len &amp;&amp; arr[left]&gt;arr[largest])&#123;<span class="comment">//先判断左节点还否超出</span></span><br><span class="line">            largest=left;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(right &lt;len &amp;&amp; arr[right]&gt;largest)&#123;<span class="comment">//右节点是否超出 找出最大的子节点</span></span><br><span class="line">            largest=right;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(largest != i)&#123;</span><br><span class="line">            swap(arr,i,largest);<span class="comment">//交换 largrst为i，这里交换的是两者序数对应的值</span></span><br><span class="line">            heapify(arr,largest);</span><br><span class="line"><span class="comment">//递归调整，如果左侧为largest，那么将左侧节点提上去，会影响左侧子树的原本正确排序，因此。递归调整左侧；递归调用的参数是，为最大的那一个</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">         * </span></span><br><span class="line"><span class="comment">          *<span class="doctag">@Description</span>:交换</span></span><br><span class="line"><span class="comment">          *<span class="doctag">@param <span class="variable">arr</span></span></span></span><br><span class="line"><span class="comment">         */</span></span><br><span class="line">    <span class="function"><span class="keyword">function</span> <span class="title">swap</span>(<span class="params">arr,i,j</span>)</span>&#123;</span><br><span class="line">        <span class="keyword">var</span> temp=arr[i];</span><br><span class="line">        arr[i] = arr[j];</span><br><span class="line">        arr[j] = temp;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="归并排序"><a href="#归并排序" class="headerlink" title="归并排序"></a>归并排序</h2><p>归并排序采用的是分治的思想，首先是“分”，将一个数组反复二分为两个小数组，</p>
<p>直到每个数组只有一个元素；其次是“治”，从最小数组开始，两两按大小顺序合并，直到并为原始数组大小，下面是图解：</p>
<p>分”就是将原始数组逐次二分，直到每个数组只剩一个元素，一个元素的数组自然是有序的，所以就可以开始“治”的过程了。</p>
<p>时间复杂度分析：</p>
<p>分的过程需要三步：log8 = 3，而每一步都需要遍历一次8个元素，所以8个元素共需要运行 8log8 次指令，那么对于 n 个元素，时间复杂度为 O(nlogn)。</p>
<p>“治”实际上是将已经有序的数组合并为更大的有序数组。那怎么做呢?就是创建一个新数组，比较left[0]和right[0] ，那个比较小就将那个的值放进新数组，</p>
<p>然后再继续比较left[0]和right[1]，或者是left[1]和right[0]。可以看出数组left，right都只需遍历一遍，所以对两个有序数组的排序的时间复杂度为O(n)。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mergeSort</span> (<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">let</span> len = arr.length</span><br><span class="line">    <span class="keyword">if</span> (len &lt; <span class="number">2</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> arr</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">let</span> middle = <span class="built_in">Math</span>.floor(len/<span class="number">2</span>)</span><br><span class="line">    <span class="comment">//拆分成两个子数组</span></span><br><span class="line">    <span class="keyword">let</span> left =  arr.slice(<span class="number">0</span>, middle)</span><br><span class="line">    <span class="keyword">let</span> right = arr.slice(middle,len)</span><br><span class="line">    <span class="comment">//递归拆分</span></span><br><span class="line">    <span class="keyword">let</span> mergeSortLeft = mergeSort(left)</span><br><span class="line">    <span class="keyword">let</span> mergeSortRight = mergeSort(right)</span><br><span class="line">    <span class="comment">//合并</span></span><br><span class="line">    <span class="keyword">return</span> merge( mergeSortLeft,mergeSortRight)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">const</span> merge = <span class="function">(<span class="params">left, right</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">const</span> result = [];</span><br><span class="line">    <span class="keyword">while</span> (left.length &amp;&amp; right.length) &#123;</span><br><span class="line">        <span class="comment">// 注意: 判断的条件是小于或等于，如果只是小于，那么排序将不稳定.</span></span><br><span class="line">        <span class="keyword">if</span> (left[<span class="number">0</span>] &lt;= right[<span class="number">0</span>]) &#123;</span><br><span class="line">            result.push(left.shift()); <span class="comment">//每次都要删除left或者right的第一个元素，将其加入result中</span></span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            result.push(right.shift());</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//将剩下的元素加上</span></span><br><span class="line">    <span class="keyword">while</span> (left.length) result.push(left.shift());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (right.length) result.push(right.shift());</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> result;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h2 id="快速排序"><a href="#快速排序" class="headerlink" title="快速排序"></a>快速排序</h2><p>希尔排序相当于是直接插入排序的升级，它们同属于插入排序类；堆排序相当于简单选择排序的升级，它们同属于选择排序类；而快速排序就是冒泡排序的升级，同属于交换排序类。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*快速排序</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">var</span> quickSort = <span class="function"><span class="keyword">function</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    　　<span class="keyword">if</span> (arr.length &lt;= <span class="number">1</span>) &#123; <span class="keyword">return</span> arr; &#125;</span><br><span class="line">    　　<span class="keyword">var</span> pivotIndex = <span class="built_in">Math</span>.floor(arr.length / <span class="number">2</span>);<span class="comment">//math.floor是-对数值的向下取整</span></span><br><span class="line">    　　<span class="keyword">var</span> pivot = arr.splice(pivotIndex, <span class="number">1</span>)[<span class="number">0</span>];</span><br><span class="line">    　　<span class="keyword">var</span> left = [];</span><br><span class="line">    　　<span class="keyword">var</span> right = [];</span><br><span class="line">    　　<span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; arr.length; i++)&#123;</span><br><span class="line">    　　　　<span class="keyword">if</span> (arr[i] &lt; pivot) &#123;</span><br><span class="line">    　　　　　　left.push(arr[i]);</span><br><span class="line">    　　　　&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    　　　　　　right.push(arr[i]);</span><br><span class="line">    　　　　&#125;</span><br><span class="line">    　　&#125;</span><br><span class="line">    　　<span class="keyword">return</span> quickSort(left).concat([pivot], quickSort(right));</span><br><span class="line">    &#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> quickSort = <span class="function"><span class="keyword">function</span>(<span class="params">arr</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (arr.length &lt;= <span class="number">1</span>) <span class="keyword">return</span> arr;</span><br><span class="line">    <span class="keyword">let</span> midIndex = <span class="built_in">Math</span>.floor(arr.length / <span class="number">2</span>);</span><br><span class="line">    <span class="keyword">let</span> mid = arr.splice(midIndex, <span class="number">1</span>)[<span class="number">0</span>];</span><br><span class="line">    <span class="keyword">let</span> left = [];</span><br><span class="line">    <span class="keyword">let</span> right = [];</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (arr[i] &lt; mid) &#123;</span><br><span class="line">            left.push(arr[i]);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            right.push(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> quickSort(left).concat([mid], quickSort(right));</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*简单改进后的快排 */</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 快排改进——黄佳新</span></span><br><span class="line">    <span class="keyword">var</span> devide_Xin = <span class="function"><span class="keyword">function</span> (<span class="params">array, start, end</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(start &gt;= end) <span class="keyword">return</span> array;</span><br><span class="line">        <span class="keyword">var</span> baseIndex = <span class="built_in">Math</span>.floor((start + end) / <span class="number">2</span>), <span class="comment">// 基数索引</span></span><br><span class="line">             i = start,</span><br><span class="line">             j = end;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">while</span> (i &lt;= j) &#123;</span><br><span class="line">            <span class="keyword">while</span> (array[i] &lt; array[baseIndex]) &#123;</span><br><span class="line">                i++;</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">while</span> (array[j] &gt; array[baseIndex])  &#123;</span><br><span class="line">                j--;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span>(i &lt;= j) &#123;</span><br><span class="line">                <span class="keyword">var</span> temp = array[i];</span><br><span class="line">                array[i] = array[j];</span><br><span class="line">                array[j] = temp;</span><br><span class="line">                i++;</span><br><span class="line">                j--;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> i;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">var</span> quickSort_Xin = <span class="function"><span class="keyword">function</span> (<span class="params">array, start, end</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(array.length &lt; <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> array;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">var</span> index = devide_Xin(array, start, end);</span><br><span class="line">        <span class="keyword">if</span>(start &lt; index <span class="number">-1</span>) &#123;</span><br><span class="line">            quickSort_Xin(array, start, index - <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(end &gt; index) &#123;</span><br><span class="line">            quickSort_Xin(array, index, end);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">return</span> array;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


    </div>
    
    
    
    
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>

      
    </div>
        <div class="reward-container">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/alipay.png" alt="Li Yudong 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Li Yudong
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://example.com/2021/09/30/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E6%9F%A5%E6%89%BE%E4%B8%8E%E6%8E%92%E5%BA%8F/" title="查找与排序">http://example.com/2021/09/30/设计模式/查找与排序/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>

    
      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/" rel="tag"># 设计模式</a>
          </div>
    
        

    
        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/09/28/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/%E5%A4%A7%E8%AF%9D%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/" rel="prev" title="大话数据结构">
      <i class="fa fa-chevron-left"></i> 大话数据结构
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/10/03/webgl/three.js%E7%B2%92%E5%AD%90%E4%B8%8E%E7%B2%BE%E7%81%B5/" rel="next" title="three.js粒子">
      three.js粒子 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#查找"><span class="nav-number">1.</span> <span class="nav-text">查找</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#顺序表查找"><span class="nav-number">1.1.</span> <span class="nav-text">顺序表查找</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#有序表查找"><span class="nav-number">1.2.</span> <span class="nav-text">有序表查找</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#折半查找"><span class="nav-number">1.2.1.</span> <span class="nav-text">折半查找</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#插值查找"><span class="nav-number">1.2.2.</span> <span class="nav-text">插值查找</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#斐波那契查找"><span class="nav-number">1.2.3.</span> <span class="nav-text">斐波那契查找</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#线性索引查找"><span class="nav-number">1.3.</span> <span class="nav-text">线性索引查找</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#稠密索引"><span class="nav-number">1.3.1.</span> <span class="nav-text">稠密索引</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#分块索引"><span class="nav-number">1.3.2.</span> <span class="nav-text">分块索引</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#倒排索引"><span class="nav-number">1.3.3.</span> <span class="nav-text">倒排索引</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#二叉排序树"><span class="nav-number">1.4.</span> <span class="nav-text">二叉排序树</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#平衡二叉树"><span class="nav-number">1.5.</span> <span class="nav-text">平衡二叉树</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#多路查找树（B树）"><span class="nav-number">1.6.</span> <span class="nav-text">多路查找树（B树）</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#B树"><span class="nav-number">1.6.1.</span> <span class="nav-text">B树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#B-树"><span class="nav-number">1.6.2.</span> <span class="nav-text">B+树</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#散列表"><span class="nav-number">1.7.</span> <span class="nav-text">散列表</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#排序"><span class="nav-number">2.</span> <span class="nav-text">排序</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#排序的分类"><span class="nav-number">2.1.</span> <span class="nav-text">排序的分类</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#冒泡排序"><span class="nav-number">2.2.</span> <span class="nav-text">冒泡排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#希尔排序"><span class="nav-number">2.3.</span> <span class="nav-text">希尔排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#堆排序"><span class="nav-number">2.4.</span> <span class="nav-text">堆排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#归并排序"><span class="nav-number">2.5.</span> <span class="nav-text">归并排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#快速排序"><span class="nav-number">2.6.</span> <span class="nav-text">快速排序</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Li Yudong"
      src="/images/author.jpg">
  <p class="site-author-name" itemprop="name">Li Yudong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">95</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/tiarmor1" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;tiarmor1" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:1157019137@qq.com" title="E-Mail → mailto:1157019137@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2020 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Yudong</span>
</div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,0' opacity='0.5' zIndex='-1' count='150' src="/lib1/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib1/anime.min.js"></script>
  <script src="/lib1/velocity/velocity.min.js"></script>
  <script src="/lib1/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  

  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;" ></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/fireworks.js"></script>
  
</body>
</html>

